function editWikiPage(idPage, content) {
	var editForm;
	var divId = '#annot' + idPage;
	var divToChange = $(divId);

	var divContent = '<form id="editAnnotationForm" action="#" onSubmit="return saveEditedAnnotation(' + idPage + ');" method="post">';
	divContent += '<textarea name="content" id="content" rows=3 cols=30 >' + content + '</textarea>';
	divContent += '<input type="submit"/>';
	divContent += '</form>';

	divToChange.html(divContent);
}

function deleteWikiPage(idPage) {
	var success;
	//Pop up to avoid unintentional deletes
	if (confirm("Are you sure you want to delete this annotation ?")) {
		$.ajax({
			url : "deleteAnnotationForm.php",
			data : {
				id : idPage
			},
			async : false,
			dataType : "text"
		}).done(function(res) {
			success = parseInt(res);
		});

	var divContent;
	//if one annotation has been deleted, show success sentence
		if (success == 1) {
			divContent = "This annotation has been successfully removed.<br/> It will disappear on reload.";
		}else{
			//otherwise, show error
			divContent = "An error happened during the conversion, and "+success+" annotations where deleted.<br/>If this keeps happenning, please contact the administrator.";
		}
		var divToChange = $('#annot' + idPage);
		divToChange.html(divContent);
	}
	return false;
}

function saveEditedAnnotation(idPage) {
	var content;

	$.ajax({
		url : "editAnnotationForm.php",
		data : {
			newContent : $('#editAnnotationForm > textarea').val(),
			id : idPage
		},
		async : false,
		dataType : "text"
	}).done(function(res) {
		content = res.trim();
	});

	var divToChange = $('#annot' + idPage);
var divContent = '<p>'+content+'</p>';
	divContent += '<button onclick="editWikiPage(' + idPage + ', \'' + content + '\')">Edit this Annotation</button><br/>';
	divContent += '<button onclick="deleteWikiPage(' + idPage + ')">Delete this Annotation</button>';
	//divContent += ' </div>';

	divToChange.html(divContent);
	return false;
}
